using System;
using System.Collections.Generic;

namespace Wantn.Share
{
	public sealed class SamplingPosGen
	{
		private const int int_0 = 6;

		private const int int_1 = 3;

		private int int_2;

		private int int_3;

		private int int_4;

		private int int_5;

		private int int_6;

		private int int_7;

		private int int_8;

		public int ColCount => 6;

		public int RowCount => 3;

		public SamplingPosGen(int xMin, int xMax, int yMin, int yMax, int zMin, int zMax, int xMinSpace)
		{
			int_2 = xMin;
			int_3 = xMax;
			int_4 = yMin;
			int_5 = yMax;
			int_6 = zMin;
			int_7 = zMax;
			int_8 = xMinSpace;
		}

		public List<SamplingPos> MakeSamplingPos(int posCount, SamplingPosMakeType makeType)
		{
			if (posCount < 1)
			{
				throw new Exception("采样点数不能<1");
			}
			if (posCount > 18)
			{
				throw new Exception("采样点数不能>" + 18 + "点");
			}
			List<SamplingBlock> blocks = method_0(6, 3, int_2, int_3, int_4, int_5, int_6, int_7, int_8);
			List<SamplingBlock> blocks2 = method_6(blocks, posCount, makeType);
			List<SamplingPos> selectedPos = method_7(blocks2);
			return method_8(selectedPos);
		}

		private List<SamplingBlock> method_0(int int_9, int int_10, int int_11, int int_12, int int_13, int int_14, int int_15, int int_16, int int_17)
		{
			if (int_9 < 1)
			{
				throw new Exception("分块的列数不能<1");
			}
			if (int_10 < 1)
			{
				throw new Exception("分块的行数不能<1");
			}
			if (int_11 < 0)
			{
				throw new Exception("X坐标的最小值不能<0");
			}
			if (int_12 < 0)
			{
				throw new Exception("X坐标的最大值不能<0");
			}
			if (int_11 >= int_12)
			{
				throw new Exception("X坐标的最小值不能>=X坐标的最大值");
			}
			if (int_13 < 0)
			{
				throw new Exception("Y坐标的最小值不能<0");
			}
			if (int_14 < 0)
			{
				throw new Exception("Y坐标的最大值不能<0");
			}
			if (int_13 >= int_14)
			{
				throw new Exception("Y坐标的最小值不能>=Y坐标的最大值");
			}
			if (int_15 < 0)
			{
				throw new Exception("Z坐标的最小值不能<0");
			}
			if (int_16 < 0)
			{
				throw new Exception("Z坐标的最大值不能<0");
			}
			if (int_15 > int_16)
			{
				throw new Exception("Z坐标的最小值不能>Z坐标的最大值");
			}
			if (int_17 > (int_12 - int_11) / int_9)
			{
				throw new Exception("X坐标最小间隔不能超过X坐标范围");
			}
			List<SamplingBlock> list = new List<SamplingBlock>();
			for (int i = 0; i < int_9; i++)
			{
				for (int j = 0; j < int_10; j++)
				{
					int blockNO = i * int_10 + 1 + j;
					int num = int_11 + i * ((int_12 - int_11) / int_9);
					if (i > 0)
					{
						num++;
					}
					int num2 = int_11 + (i + 1) * ((int_12 - int_11) / int_9);
					if (num2 > int_12 || i == int_9 - 1)
					{
						num2 = int_3;
					}
					int num3 = int_13 + j * ((int_14 - int_4) / int_10);
					if (j > 0)
					{
						num3++;
					}
					int num4 = int_13 + (j + 1) * ((int_14 - int_13) / int_10);
					if (num4 > int_14 || j == int_10 - 1)
					{
						num4 = int_14;
					}
					SamplingBlock item = new SamplingBlock(blockNO, j, i, num, num2, num3, num4, int_15, int_16);
					list.Add(item);
				}
			}
			list.Sort();
			smethod_0(list, "生成块：");
			return list;
		}

		private List<SamplingBlock> method_1(List<SamplingBlock> bolcks, int int_9)
		{
			List<SamplingBlock> list = new List<SamplingBlock>();
			for (int i = 0; i < bolcks.Count; i++)
			{
				if (bolcks[i].RowNO == int_9)
				{
					list.Add(bolcks[i]);
				}
			}
			list.Sort();
			return list;
		}

		private List<SamplingBlock> method_2(List<SamplingBlock> bolcks, int int_9)
		{
			List<SamplingBlock> list = new List<SamplingBlock>();
			for (int i = 0; i < bolcks.Count; i++)
			{
				if (bolcks[i].ColNO == int_9)
				{
					list.Add(bolcks[i]);
				}
			}
			list.Sort();
			return list;
		}

		private List<SamplingBlock> method_3(List<SamplingBlock> blocksSrc, List<SamplingBlock> blocksFilter, int[] int_9)
		{
			List<SamplingBlock> list = new List<SamplingBlock>();
			for (int i = 0; i < 6; i++)
			{
				bool flag = false;
				if (int_9 != null && int_9.Length > 0)
				{
					for (int j = 0; j < int_9.Length; j++)
					{
						if (i == int_9[j])
						{
							flag = true;
							break;
						}
					}
				}
				if (!flag)
				{
					list.AddRange(method_2(blocksSrc, i));
				}
			}
			return method_4(list, blocksFilter);
		}

		private List<SamplingBlock> method_4(List<SamplingBlock> blocksSrc, List<SamplingBlock> blocksFilter)
		{
			List<SamplingBlock> list = new List<SamplingBlock>();
			list.AddRange(blocksSrc);
			if (blocksFilter != null && blocksFilter.Count > 0 && list != null && list.Count > 0)
			{
				for (int i = 0; i < blocksFilter.Count; i++)
				{
					for (int j = 0; j < list.Count; j++)
					{
						if (list[j].BlockNO == blocksFilter[i].BlockNO)
						{
							list.Remove(list[j]);
							break;
						}
					}
				}
			}
			return list;
		}

		private int[] method_5(SamplingBlock samplingBlock_0)
		{
			if (samplingBlock_0.ColNO <= 1)
			{
				return new int[2]
				{
					0,
					1
				};
			}
			if (samplingBlock_0.ColNO <= 3)
			{
				return new int[2]
				{
					2,
					3
				};
			}
			if (samplingBlock_0.ColNO <= 5)
			{
				return new int[2]
				{
					4,
					5
				};
			}
			return null;
		}

		private List<SamplingBlock> method_6(List<SamplingBlock> blocks, int int_9, SamplingPosMakeType samplingPosMakeType_0)
		{
			SamplingBlock samplingBlock = null;
			List<SamplingBlock> list = new List<SamplingBlock>();
			List<SamplingBlock> list2 = new List<SamplingBlock>();
			List<SamplingBlock> list3 = new List<SamplingBlock>();
			list3.AddRange(blocks);
			switch (samplingPosMakeType_0)
			{
			case SamplingPosMakeType.GB_FullRand:
				list.AddRange(RandSampling.Make(list3, int_9, duplicate: false));
				break;
			case SamplingPosMakeType.GB_EquaRand:
			{
				for (int j = 0; j < int_9; j++)
				{
					samplingBlock = RandSampling.Make(list3, 1, duplicate: false)[0];
					list.Add(samplingBlock);
					list2.AddRange(method_2(list3, samplingBlock.ColNO));
					list3 = method_4(list3, list2);
					if (j >= 5 && int_9 > 6)
					{
						list3 = method_4(blocks, list);
					}
				}
				break;
			}
			case SamplingPosMakeType.WT_3PartRand:
			{
				for (int i = 0; i < int_9; i++)
				{
					samplingBlock = RandSampling.Make(list3, 1, duplicate: false)[0];
					list.Add(samplingBlock);
					list2.Add(samplingBlock);
					list3 = method_3(list3, list2, method_5(samplingBlock));
					if (i >= 2 && int_9 > 3)
					{
						list3 = method_4(blocks, list);
						foreach (SamplingBlock item in list)
						{
							list2.AddRange(method_2(list3, item.ColNO));
							list3 = method_4(list3, list2);
						}
						if (list3.Count < 1)
						{
							list3 = method_4(blocks, list);
						}
					}
				}
				break;
			}
			}
			list.Sort();
			smethod_0(list, "抽取随机块：");
			return list;
		}

		private List<SamplingPos> method_7(List<SamplingBlock> blocks)
		{
			List<SamplingPos> list = new List<SamplingPos>();
			foreach (SamplingBlock block in blocks)
			{
				List<int> list2 = new List<int>();
				for (int i = block.MinX; i < block.MaxX; i++)
				{
					list2.Add(i);
				}
				int x = RandSampling.Make(list2, 1, duplicate: true)[0];
				List<int> list3 = new List<int>();
				for (int j = block.MinY; j < block.MaxY; j++)
				{
					list3.Add(j);
				}
				int y = RandSampling.Make(list3, 1, duplicate: true)[0];
				List<int> list4 = new List<int>();
				for (int k = block.MinZ; k < block.MaxZ; k++)
				{
					list4.Add(k);
				}
				int num = 0;
				SamplingPos item = new SamplingPos(z: (list4.Count > 1) ? RandSampling.Make(list4, 1, duplicate: true)[0] : block.MinZ, blockNO: block.BlockNO, rowNO: block.RowNO, colNO: block.ColNO, minX: block.MinX, maxX: block.MaxX, minY: block.MinY, maxY: block.MaxY, minZ: block.MinZ, maxZ: block.MaxZ, x: x, y: y);
				list.Add(item);
			}
			list.Sort();
			cyImSjaWI(list, "抽取随机坐标：");
			return list;
		}

		private List<SamplingPos> method_8(List<SamplingPos> selectedPos)
		{
			List<SamplingPos> list = new List<SamplingPos>();
			list.AddRange(selectedPos);
			if (list.Count > 1)
			{
				list.Sort();
				bool flag = false;
				for (int i = 0; i < list.Count; i++)
				{
					if (i <= 0)
					{
						continue;
					}
					int num = int_8 - (list[i].X - list[i - 1].X);
					if (num > 0)
					{
						list[i].X += num;
						if (list[i].X > list[i].MaxX)
						{
							list[i].X = list[i].MaxX;
						}
						flag = true;
					}
				}
				if (flag)
				{
					list.Sort();
					cyImSjaWI(list, "调整随机坐标：");
				}
			}
			return list;
		}

		private static void smethod_0(List<SamplingBlock> blocks, object object_0)
		{
		}

		private static void cyImSjaWI(List<SamplingPos> poss, object object_0)
		{
		}
	}
}
